package com.roads.project.controller;

import java.util.Iterator;
import java.util.List;

import com.roads.common.constant.UserConstants;
import com.roads.common.utils.StringUtils;
import com.roads.project.domain.ProBoq;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.roads.common.annotation.Log;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.enums.BusinessType;
import com.roads.project.domain.ProMaterialBoq;
import com.roads.project.service.IProMaterialBoqService;
import com.roads.common.utils.poi.ExcelUtils;
import com.roads.common.core.page.TableDataInfo;

/**
 * 材料清单Controller
 * 
 * @author nhp
 * @date 2022-01-02
 */
@RestController
@RequestMapping("/project/material")
public class ProMaterialBoqController extends BaseController {

    @Autowired
    private IProMaterialBoqService service;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('project:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProMaterialBoq model) {
        startPage();
        List<ProMaterialBoq> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 查询树形列表
     */
    @GetMapping("/getTreeList")
    public AjaxResult getTreeList() {
        List<ProMaterialBoq> list = service.selectTreeListFormatName();
        return AjaxResult.success(list);
    }

    /**
     * 查询树形列表 排除子项
     */
    @GetMapping("/getTreeListExcludeChild")
    public AjaxResult getTreeListExcludeChild(ProMaterialBoq model) {
        List<ProMaterialBoq> list = service.selectTreeListFormatName();
        model = service.getModelById(model.getBoqId());
        Iterator<ProMaterialBoq> it = list.iterator();
        while (it.hasNext()) {
            ProMaterialBoq b = it.next();
            if (b.getBoqId().equals(model.getBoqId()) ||
                    ArrayUtils.contains(StringUtils.split(b.getAncestors(), ","), model.getBoqCode() + "")) {
                it.remove();
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('project:material:export')")
    @Log(title = "材料清单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProMaterialBoq model) {
        List<ProMaterialBoq> list = service.selectModelList(model);
        ExcelUtils<ProMaterialBoq> util = new ExcelUtils<ProMaterialBoq>(ProMaterialBoq.class);
        return util.exportExcel(list, "材料清单数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:material:query')")
    @GetMapping(value = "/{boqId}")
    public AjaxResult getInfo(@PathVariable("boqId") Long boqId) {
        return AjaxResult.success(service.getModelById(boqId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('project:material:add')")
    @Log(title = "材料清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProMaterialBoq model) {
        if (UserConstants.NOT_UNIQUE.equals(service.checkBoqCodeUnique(model))) {
            return AjaxResult.error("新增材料清单'" + model.getBoqCode() + "'失败，材料清单编号已存在");
        }
        model.setCreateBy(getUsername());
        return toAjax(service.insert(model));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('project:material:edit')")
    @Log(title = "材料清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProMaterialBoq model) {
        model.setUpdateBy(getUsername());
        return toAjax(service.update(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('project:material:remove')")
    @Log(title = "材料清单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{boqId}")
    public AjaxResult remove(@PathVariable("boqId") Long boqId) {
        ProMaterialBoq materialBoq = service.getModelById(boqId);
        if (service.getCountChildren(materialBoq.getBoqCode()) > 0) {
            return AjaxResult.error("存在下级清单,不允许删除");
        }
        return toAjax(service.delete(materialBoq, boqId));
    }
}
